<?php
class Shop_ProductController extends Promotor_Controller_Action_Scaffold {
	public $skin = array(
		// forsuje domyslny template i style na admin
		'template' => 'admin',
		'style' => 'default',
		// layout
		'layout' => 'admin_shop',
		// rozszerzam konfiguracje
		'config' => array(
			'filename' => 'backend_config.ini'
		),
		'display' => array(
			'personalized' => true,
			'layout' => 'shop_product_display'
		)
	);

	public $acl = array(
		'list' => 'list',
		'add' => 'create',
		'edit' => 'update',
		'delete' => 'delete',
		'display' => 'show'
	);
	
	/**
	 * @var array
	 */
	protected $_redirectAction = array(
		'add' => array('save','next','exit', 'params' => array('setId')),
		'edit' => array('save','next','exit'),
		'delete' => array('exit'),
	);

	protected $_redirectActionParams = array(
		'add' => array('next' => array('attr_set_id'), 'save' => array('attr_set_id')),
		'edit' => array('next' => array('attr_set_id'), 'save' => array('attr_set_id')),
	);
	
	/**
	 * @see Promotor_Controller_Action_Scaffold specialization
	 */
	protected $_modelClass = 'Shop_Model_Product';
	protected $_formAddClass = 'Shop_Form_Product_Add';
	protected $_formEditClass = 'Shop_Form_Product_Edit';
	protected $_formRemoveClass = 'Shop_Form_Product_Remove';
	// end of specialization
	
	/**
	 * @return void
	 */
	public function indexAction() {
		$this->_forward('list');
	}

	/**
	 * Wyświetlanie produkty
	 * @return void
	 */
	public function displayAction() {
		$model = new Shop_Model_Product();
		$image = new Shop_Model_Image();
		$manufacturer = new Shop_Model_Manufacturer();
		$attribs = new Shop_Model_Attr();

		/* @var $rq Zend_Controller_Request_Http */
		$rq = $this->getRequest();

		$alias = $rq->getParam('alias', $rq->getParam('id'));
		$this->view->alias = $alias;

		$row = $model->findByIdAllInfoCache($alias);

		$this->_addMessages($model->getMessages());

			if (!is_array($row)) {
				/* @var $renderer Zend_Controller_Action_Helper_ViewRenderer */
				$renderer = $this->_helper->getHelper('ViewRenderer');
				$renderer->render('display.error');
				return;
			}

		$this->view->row = $row;
		
		$this->view->images = $image->findAllByProductIdCache($row['id']);
		$this->view->manufacturer = $manufacturer->findByIdCache($row['manufacturer_id']);
		$this->view->attribs = $attribs->findByProductIdCache($row['id']);
	}
}